package com.roger.tool.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;

public class HttpGetUtils {

	public static final int ERROR_CODE_SUCCEED = 1000;
	public static final int ERROR_CODE_TIMEOUT = 1001;
	public static final int ERROR_CODE_UNKNOWN = 1002;

	/**
	 * 获取流文件
	 * 
	 * @param pUrl
	 *            文件url
	 * @return 网络文件流文件
	 * @throws IOException
	 */
	private static InputStream httpGetInputStream(HttpGetUrlParam pUrlParam) throws IOException {
		InputStream input = null;
		URL connectURL = new URL(pUrlParam.toString());
		HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
		// conn.setRequestMethod("POST");
		conn.setReadTimeout(50000);
		conn.setConnectTimeout(50000);
		input = conn.getInputStream();
		return input;
	}

	/**
	 * 获得网络文本文件的内容
	 * 
	 * @param pUrl
	 *            文本文件的url
	 * @return 文本文件的内容
	 */
	public static String httpGetData(HttpGetUrlParam pUrlParam) {
		return HttpGetUtils.httpGetData(pUrlParam, null, null);
	}

	public static String httpGetData(HttpGetUrlParam pUrlParam, Object pUserData, IOnHttpGetListener pOnHttpGetListener) {
//		LogSdk.v("url:"+ pUrlParam.toString());
		StringBuffer sb = new StringBuffer("");
		try {
			InputStream input = httpGetInputStream(pUrlParam);
			BufferedReader bfr = new BufferedReader(new InputStreamReader(input));
			String line = "";
			while ((line = bfr.readLine()) != null) {
				sb.append(line);
			}
			try {
				bfr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (pOnHttpGetListener != null) {
				pOnHttpGetListener.onHttpGetResult(pUserData, sb.toString(), ERROR_CODE_SUCCEED);
			}
			return sb.toString();
		} catch (SocketTimeoutException e) {
			// TODO: handle exception
			e.printStackTrace();
			if (pOnHttpGetListener != null) {
				pOnHttpGetListener.onHttpGetResult(pUserData, sb.toString(), ERROR_CODE_TIMEOUT);
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if (pOnHttpGetListener != null) {
				pOnHttpGetListener.onHttpGetResult(pUserData, sb.toString(), ERROR_CODE_UNKNOWN);
			}
			return null;
		}
	}

	public static void httpGetDataInThread(HttpGetUrlParam pUrlParam) {
		HttpGetUtils.httpGetDataInThread(pUrlParam, null, null);
	}

	public static void httpGetDataInThread(final HttpGetUrlParam pUrlParam, final Object pUserData, final IOnHttpGetListener pOnHttpGetListener) {
		Thread thread = new Thread() {

			public void run() {
				HttpGetUtils.httpGetData(pUrlParam, pUserData, pOnHttpGetListener);
			}

		};
		thread.setName("getHttpContent");
		thread.start();
	}

	public static class HttpGetUrlParam {

		private String mUrl = "";

		private Map<String, String> mUrlParamMap = new HashMap<String, String>();

		public HttpGetUrlParam(String pUrl) {
			this.mUrl = pUrl;
		}

		public void put(String pKey, Object pValue) {
			this.mUrlParamMap.put(encodeData(pKey), encodeData(pValue));
		}

		public void putAll(HttpGetUrlParam pHttpGetUrlParam) {
			if (pHttpGetUrlParam != null) {
				Map<String, String> urlParamMap = pHttpGetUrlParam.getAllUrlParamMap();
				if (urlParamMap != null && urlParamMap.size() > 0) {
					this.mUrlParamMap.putAll(urlParamMap);
				}
			}
		}

		public Map<String, String> getAllUrlParamMap() {
			return mUrlParamMap;
		}

		public String toString() {
			if (this.mUrlParamMap.size() <= 0) {
				return this.mUrl;
			} else {
				StringBuffer urlParamBuffer = new StringBuffer("?");
				for (String key : this.mUrlParamMap.keySet()) {
					try {
						String value = this.mUrlParamMap.get(key);
						urlParamBuffer.append(key).append("=").append(value).append("&");
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				urlParamBuffer.deleteCharAt(urlParamBuffer.length() - 1);
				return this.mUrl + urlParamBuffer.toString();
			}
		}

		private String encodeData(Object s) {
			try {
				return URLEncoder.encode(String.valueOf(s), "UTF-8");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "";
			}
		}

	}

	public interface IOnHttpGetListener {

		public void onHttpGetResult(Object pUserData, String pHttpContent, int pErrorCode);

	}

}
